package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.system.domain.Requisitions;
import com.ruoyi.system.mapper.ApplianceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AppliDetailMapper;
import com.ruoyi.system.domain.AppliDetail;
import com.ruoyi.system.service.IAppliDetailService;

/**
 * 租赁采购Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-30
 */
@Service
public class AppliDetailServiceImpl implements IAppliDetailService 
{
    @Autowired
    private AppliDetailMapper appliDetailMapper;

    /**
     * 查询租赁采购
     * 
     * @param appliId 租赁采购ID
     * @return 租赁采购
     */
    @Override
    public AppliDetail selectAppliDetailById(Long appliId)
    {
        return appliDetailMapper.selectAppliDetailById(appliId);
    }

    /**
     * 查询租赁采购列表
     * 
     * @param appliDetail 租赁采购
     * @return 租赁采购
     */
    @Override
    public List<AppliDetail> selectAppliDetailList(AppliDetail appliDetail)
    {
        return appliDetailMapper.selectAppliDetailList(appliDetail);
    }

    /**
     * 新增租赁采购
     * 备注
     * @param appliDetail 租赁采购
     * @return 结果
     */
    @Override
    public int insertAppliDetail(AppliDetail appliDetail) throws ParseException {
        Double sum=0D;
        int result=appliDetailMapper.insertAppliDetail(appliDetail);
        if (null!=appliDetail.getAppliDetailMats()){
            for (int i = 0; i < appliDetail.getAppliDetailMats().size(); i++) {
                appliDetail.getAppliDetailMats().get(i).setAppliDetId(appliDetail.getAppliId());
                //租赁天数  计算总价
                int daysBetween = daysBetween(appliDetail.getAppliDetailMats().get(i).getAppliStartTime(), appliDetail.getAppliDetailMats().get(i).getAppliEndTime())+1;
                Double appliPrice = appliDetail.getAppliDetailMats().get(i).getAppliPrice();
                Double appliSum = appliDetail.getAppliDetailMats().get(i).getAppliSum();
                //总价
                sum+=(daysBetween*appliPrice*appliSum);
                appliDetailMapper.insertAppliDetailMat(appliDetail.getAppliDetailMats().get(i));
            }
            //修改总价
            appliDetail.setFullAmount(sum);
            appliDetailMapper.updateAppliDetail(appliDetail);
        }
        return result;
    }

    /**
     * 修改租赁采购
     * 
     * @param appliDetail 租赁采购
     * @return 结果
     */
    @Override
    public int updateAppliDetail(AppliDetail appliDetail) throws ParseException {
        DecimalFormat df = new DecimalFormat("######0.00");
        df.setRoundingMode(RoundingMode.HALF_UP);
        int result = appliDetailMapper.updateAppliDetail(appliDetail);
        Double sum=0D;
        if (null!=appliDetail.getAppliDetailMats()){
            appliDetailMapper.deleteAppliDetailMat(appliDetail.getAppliId());
            for (int i = 0; i < appliDetail.getAppliDetailMats().size(); i++) {
                appliDetail.getAppliDetailMats().get(i).setAppliDetId(appliDetail.getAppliId());
                //租赁天数  计算总价
                int daysBetween = daysBetween(appliDetail.getAppliDetailMats().get(i).getAppliStartTime(), appliDetail.getAppliDetailMats().get(i).getAppliEndTime())+1;
                Double appliPrice = appliDetail.getAppliDetailMats().get(i).getAppliPrice();
                Double appliSum = appliDetail.getAppliDetailMats().get(i).getAppliSum();
                //总价
                sum+=(daysBetween*appliPrice*appliSum);
                appliDetailMapper.insertAppliDetailMat(appliDetail.getAppliDetailMats().get(i));
            }
            //修改总价
            appliDetail.setFullAmount(Double.valueOf(df.format(new BigDecimal(sum+""))));
            appliDetailMapper.updateAppliDetail(appliDetail);
        }
        return result;
    }

    /**
     * 批量删除租赁采购
     * 
     * @param appliIds 需要删除的租赁采购ID
     * @return 结果
     */
    @Override
    public int deleteAppliDetailByIds(Long[] appliIds)
    {
        return appliDetailMapper.deleteAppliDetailByIds(appliIds);
    }



    @Override
    public AppliDetail queryAppliDetailListById(Long id) throws ParseException {
        DecimalFormat df = new DecimalFormat("######0.00");
        df.setRoundingMode(RoundingMode.HALF_UP);
        AppliDetail appliDetail = appliDetailMapper.queryAppliDetailListById(id);
        if (null!=appliDetail.getAppliDetailMats()){
            for (int i = 0; i < appliDetail.getAppliDetailMats().size(); i++) {
                int daysBetween=0;
                //租赁天数  计算总价
                if (null!=appliDetail.getAppliDetailMats().get(i).getAppliStartTime()){
                    daysBetween = daysBetween(appliDetail.getAppliDetailMats().get(i).getAppliStartTime(), appliDetail.getAppliDetailMats().get(i).getAppliEndTime())+1;
                    //使用天数
                    appliDetail.getAppliDetailMats().get(i).setUseDay(daysBetween+"");
                }
                Double price = appliDetail.getAppliDetailMats().get(i).getAppliPrice();
                Double sum = appliDetail.getAppliDetailMats().get(i).getAppliSum();
                //总价
                if (null!=price&&null!=sum){
                    appliDetail.getAppliDetailMats().get(i).setSum(Double.valueOf(df.format(price*sum*daysBetween))+"");
                }
            }
        }
        return appliDetail;
    }

    public static int daysBetween(Date smdate,Date bdate) throws ParseException
    {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        smdate=sdf.parse(sdf.format(smdate));
        bdate=sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);

        return Integer.parseInt(String.valueOf(between_days));
    }

}
